//
//  SendFaceView.m
//  自定义聊天界面和键盘-多选照片-语音-表情-文本
//
//  Created by qinlongqing on 14-10-14.
//  Copyright (c) 2014年 qinlongqing. All rights reserved.
//

#import "SendFaceView.h"

@interface SendFaceView ()<UIScrollViewDelegate>
{
    NSInteger pages;
    
    UIPageControl *pageControl;
}
@end

@implementation SendFaceView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup{
#pragma 得到scrollView一共多少页
    NSDictionary *faceDataDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"expression" ofType:@"plist"]];
    NSInteger totalFace = [[faceDataDict allKeys] count];
    pages = totalFace/20;
    if (totalFace%20) {
        pages += 1;
    }
#pragma 创建UIScrollView和UIPageControl
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 135)];
    [scrollView setPagingEnabled:YES];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    scrollView.alwaysBounceHorizontal = NO;
    scrollView.alwaysBounceVertical = NO;
    [scrollView setContentSize:CGSizeMake(self.frame.size.width * pages, scrollView.frame.size.height)];
    scrollView.delegate = self;
//    scrollView.backgroundColor = [UIColor yellowColor];
    [self addSubview:scrollView];
//    [scrollView release];
    
    pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, scrollView.frame.size.height, self.frame.size.width, self.frame.size.height - scrollView.frame.size.height)];
    [pageControl setPageIndicatorTintColor:[UIColor lightGrayColor]];
    [pageControl setCurrentPageIndicatorTintColor:[UIColor grayColor]];
    pageControl.numberOfPages = pages;
    pageControl.currentPage = 0;
    pageControl.backgroundColor=[UIColor clearColor];
//    pageControl.backgroundColor = [UIColor purpleColor];
    [self addSubview:pageControl];
//    [pageControl release];
#pragma 布局表情
    for (NSInteger i=0; i<pages; i++) {
        for (NSInteger j=1; j<=21; j++) {
            NSString *faceFileName = nil;
            NSInteger index = 20*i + j;
            faceFileName = [NSString stringWithFormat:@"Expression_%ld@2x.png",index];
            if (j == 21) {
                faceFileName = @"DeleteEmoticonBtn_ios7@2x.png";
            }
            CGFloat faceFile_X = 0;
            CGFloat faceFile_Y = 0;
            
            NSInteger col = (j - 1)%7;
            NSInteger row = (j - 1)/7;
            
            faceFile_X = 2 + col*45 + (self.frame.size.width)*i;
            faceFile_Y = row*45;
            UIButton *faceButton = [[UIButton alloc]initWithFrame:CGRectMake(faceFile_X + 10, faceFile_Y + 10, 24, 24)];
            [faceButton setImage:[UIImage imageNamed:faceFileName] forState:UIControlStateNormal];
            [faceButton addTarget:self action:@selector(selectFaceView:) forControlEvents:UIControlEventTouchUpInside];
            if (j == 21) {
                faceButton.tag = 1000;
            }
            else{
                faceButton.tag = index;
            }
//            [faceButton setBackgroundColor:[UIColor clearColor]];
            [scrollView addSubview:faceButton];
//            [faceButton release];
        }
    }
    
}

- (void)selectFaceView:(UIButton *)senderButton{
    NSString *faceName;
    __block NSString *placeholderName;
    BOOL isDelete;
    if (senderButton.tag == 1000) {
        faceName = nil;
        placeholderName = nil;
        isDelete = YES;
    }
    else{
        faceName = [NSString stringWithFormat:@"Expression_%ld@2x.png",senderButton.tag];

        NSDictionary *faceDataDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"expression" ofType:@"plist"]];
        [faceDataDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            if ([obj isEqualToString:faceName]) {
                placeholderName = (NSString *)key;
                *stop = YES;
            }
        }];
        isDelete = NO;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedFaceFileName:withPlaceholderName:andIsDelete:)]) {
        [self.delegate didSelectedFaceFileName:faceName withPlaceholderName:placeholderName andIsDelete:isDelete];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger currentPage = (scrollView.contentOffset.x)/(self.frame.size.width);
    pageControl.currentPage = currentPage;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
